package com.suixun.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.suixun.dao.DevicelistMapper;
import com.suixun.dao.SchoolDiscountsMapper;
import com.suixun.dao.SchoolinfoMapper;
import com.suixun.dao.StdDeviceinfoMapper;
import com.suixun.dao.StdIotDeviceMapper;
import com.suixun.dao.SysConfigMapper;
import com.suixun.dao.devicemanage.DevicePriceServiceDAO;
import com.suixun.dao.parentmanage.ParentManageDAO;
import com.suixun.entity.Devicelist;
import com.suixun.entity.DevicelistExample;
import com.suixun.entity.ParentInfo;
import com.suixun.entity.SchoolDiscountDO;
import com.suixun.entity.SchoolDiscounts;
import com.suixun.entity.SchoolDiscountsExample;
import com.suixun.entity.Schoolinfo;
import com.suixun.entity.SchoolinfoExample;
import com.suixun.entity.StdDeviceinfo;
import com.suixun.entity.StdDeviceinfoExample;
import com.suixun.entity.StdIotDevice;
import com.suixun.entity.StdIotDeviceExample;
import com.suixun.entity.SysConfig;
import com.suixun.service.Constants;
import com.suixun.service.DevicePriceService;
import com.suixun.service.cache.CacheService;
import com.suixun.vo.DevicePriceVO;
import com.suixun.vo.SchoolDiscountVO;
import com.suixun.vo.SystemPriceVO;

import rop.utils.spring.CollectionUtils;

@Service
public class DevicePriceServiceImpl implements DevicePriceService {
	private final static Logger logger = LoggerFactory.getLogger(DevicePriceServiceImpl.class);

	@Resource
	private SysConfigMapper sysConfigMapper;
	@Resource
	private DevicePriceServiceDAO devicePriceServiceDAO;
	@Resource
	private SchoolDiscountsMapper schoolDiscountsMapper;
	@Resource
	private SchoolinfoMapper schoolinfoMapper;
	@Resource
	private DevicelistMapper devicelistMapper;
	@Resource
	private StdIotDeviceMapper stdIotDeviceMapper;
	@Resource
	private StdDeviceinfoMapper stdDeviceinfoMapper;
	@Resource
	private CacheService cacheService;
	@Resource
	private ParentManageDAO parentManageDAO;

	/**
	 * <p>
	 * Title: getSystemPrice
	 * </p>
	 * <p>
	 * 功能描述: 获取系统默认价格
	 * </p>
	 * 
	 * @return
	 */
	@Override
	public SystemPriceVO getSystemPrice() {
		HashMap<String, BigDecimal> prices = Constants.PURCHASE_DEVICE_PRICES();
		if (prices == null) {
			return null;
		}
		SystemPriceVO systemPriceVo = new SystemPriceVO();
		if (prices.containsKey("2")) {
			systemPriceVo.setSmartCardPrice(prices.get("2"));
		} else {
			systemPriceVo.setSmartCardPrice(new BigDecimal("0.0"));
		}
		if (prices.containsKey("3")) {
			systemPriceVo.setWhiteCard2_4Price(prices.get("3"));
		} else {
			systemPriceVo.setWhiteCard2_4Price(new BigDecimal("0.0"));
		}
		if (prices.containsKey("13.56")) {
			systemPriceVo.setWhiteCard13_56Price(prices.get("13.56"));
		} else {
			systemPriceVo.setWhiteCard13_56Price(new BigDecimal("0.0"));
		}
		if (prices.containsKey("packagefee")) {
			systemPriceVo.setPackagePrice(prices.get("packagefee"));
		} else {
			systemPriceVo.setPackagePrice(new BigDecimal("0.0"));
		}
		return systemPriceVo;
	}

	/**
	 * <p>
	 * Title: updateSystemPrice
	 * </p>
	 * <p>
	 * 功能描述: 修改系统价格
	 * </p>
	 * 
	 * @param systemPriceVo
	 * @return
	 */
	@Override
	public String updateSystemPrice(SystemPriceVO systemPriceVo) {
		if (systemPriceVo == null) {
			return null;
		}
		HashMap<String, BigDecimal> prices = Constants.PURCHASE_DEVICE_PRICES();
		if (prices == null) {
			prices = new HashMap<>();
		}
		if (systemPriceVo.getPackagePrice() != null) {
			prices.put("packagefee", systemPriceVo.getPackagePrice());
		}
		if (systemPriceVo.getSmartCardPrice() != null) {
			prices.put("2", systemPriceVo.getSmartCardPrice());
		}
		if (systemPriceVo.getWhiteCard13_56Price() != null) {
			prices.put("13.56", systemPriceVo.getWhiteCard13_56Price());
		}
		if (systemPriceVo.getWhiteCard2_4Price() != null) {
			prices.put("3", systemPriceVo.getWhiteCard2_4Price());
		}
		ObjectMapper mapper = new ObjectMapper();
		try {
			String priceString = mapper.writeValueAsString(prices);
			SysConfig sysConfig = new SysConfig();
			sysConfig.setValue(priceString);
			sysConfig.setSyskey("PURCHASE_DEVICE_PRICES");
			sysConfigMapper.updateByPrimaryKeySelective(sysConfig);
		} catch (JsonProcessingException e) {
			logger.error("json解析异常", e);
			return "服务器发生错误，请联系管理员";
		}
		cacheService.flushSystemPrices();
		return null;
	}

	/**
	 * <p>
	 * Title: getSchoolPrice
	 * </p>
	 * <p>
	 * 功能描述: 获取学校的价格
	 * </p>
	 * 
	 * @return
	 */
	@Override
	public List<SchoolDiscountVO> getSchoolPrices(String schoolName) {
		if (StringUtils.isEmpty(schoolName)) {
			return null;
		}
		List<SchoolDiscountDO> schoolDiscountList = devicePriceServiceDAO.getSchoolPriceByName(schoolName);
		if (CollectionUtils.isEmpty(schoolDiscountList)) {
			return null;
		}
		SchoolDiscountVO schoolDiscountVo = null;
		List<SchoolDiscountVO> schoolDiscountVoList = new ArrayList<>();
		for (SchoolDiscountDO schoolDiscountDo : schoolDiscountList) {
			schoolDiscountVo = new SchoolDiscountVO();
			schoolDiscountVo.setDevicetype(schoolDiscountDo.getDevicetype());
			schoolDiscountVo.setId(schoolDiscountDo.getId());
			schoolDiscountVo.setPackagePrice(schoolDiscountDo.getDiscountpackage());
			schoolDiscountVo.setPrice(schoolDiscountDo.getDiscountprice());
			schoolDiscountVoList.add(schoolDiscountVo);
		}
		return schoolDiscountVoList;
	}

	/**
	 * <p>
	 * Title: addSchoolPrices
	 * </p>
	 * <p>
	 * 功能描述: 新增学校促销价格
	 * </p>
	 * 
	 * @param schoolDiscountVo
	 * @return
	 */
	@Override
	@Transactional
	public String addSchoolPrices(SchoolDiscountVO schoolDiscountVo) {
		if (schoolDiscountVo == null) {
			return "学校价格为空";
		}
		if (StringUtils.isBlank(schoolDiscountVo.getSchoolName())) {
			return "学校名称为空";
		}
		Integer schoolid = getSchoolidBySchoolName(schoolDiscountVo.getSchoolName());
		if (schoolid == null) {
			return "该学校不存在";
		}
		// 新增智能学生证促销价
		if (schoolDiscountVo.getSmartCardPrice() != null) {
			addSchoolDiscount(schoolDiscountVo.getSmartCardPrice(), 2, schoolid, schoolDiscountVo.getPackagePrice());
			synPackageToDevices(schoolDiscountVo.getSchoolName(), schoolDiscountVo.getPackagePrice());
			// 新增智能学生证促销套餐
		} else if (schoolDiscountVo.getPackagePrice() != null) {
			addSchoolDiscount(new BigDecimal(0), 2, schoolid, schoolDiscountVo.getPackagePrice());
			synPackageToDevices(schoolDiscountVo.getSchoolName(), schoolDiscountVo.getPackagePrice());
		}
		// 新增13.56考勤卡价格
		if (schoolDiscountVo.getWhiteCard13_56Price() != null) {
			addSchoolDiscount(schoolDiscountVo.getWhiteCard13_56Price(), 8, schoolid, null);
		}
		// 新增2.4考勤卡价格
		if (schoolDiscountVo.getWhiteCard2_4Price() != null) {
			addSchoolDiscount(schoolDiscountVo.getWhiteCard2_4Price(), 9, schoolid, null);
		}
		// 清除缓存
		cacheService.flushDevicePrices(schoolDiscountVo.getSchoolName());
		return null;
	}

	/**
	 * <p>
	 * Title: synPackageToDevices
	 * </p>
	 * <p>
	 * 功能描述:
	 * 同步该学校下设备的价格,因为用户购买设备的时候是不会针对某一个设备购买的，所以设备的硬件价格只需要保存在学校促销价或者是系统默认价就行，不需要同步
	 * </p>
	 * 
	 * @param schoolName
	 * @param packagePrice
	 */
	private void synPackageToDevices(String schoolName, BigDecimal packagePrice) {
		// 修改devicelist表中的套餐价格
		Map<String, Object> map = new HashMap<>();
		map.put("schoolName", schoolName);
		map.put("packagePrice", packagePrice);
		devicePriceServiceDAO.updateDeviceListPrices(map);

		// 修改std_deviceinfo表中的套餐价格
		map = new HashMap<>();
		map.put("schoolName", schoolName);
		map.put("deviceType", 2);
		List<Devicelist> devicelistList = devicePriceServiceDAO.getDevicelistsByParam(map);
		if (!CollectionUtils.isEmpty(devicelistList)) {
			map = new HashMap<>();
			map.put("devicelistList", devicelistList);
			map.put("packagePrice", packagePrice);
			devicePriceServiceDAO.updateStdDeviceinfoPrices(map);
		}

		// 修改std_iot_device表
		map = new HashMap<>();
		map.put("schoolName", schoolName);
		map.put("packagePrice", packagePrice);
		devicePriceServiceDAO.updateStdIotDevicePrices(map);
	}

	private void addSchoolDiscount(BigDecimal price, Integer devicetype, Integer schoolid, BigDecimal discountpackage) {
		// 判断该学校是否已经添加该相同设备的促销了，若是，则修改，反之，再新增
		SchoolDiscountsExample schoolDiscountsExample = new SchoolDiscountsExample();
		schoolDiscountsExample.createCriteria().andSchoolidEqualTo(schoolid).andDevicetypeEqualTo(devicetype);
		if (!CollectionUtils.isEmpty(schoolDiscountsMapper.selectByExample(schoolDiscountsExample))) {
			SchoolDiscounts schoolDiscounts = new SchoolDiscounts();
			schoolDiscounts.setUpdatetime(new Date());
			if ("2".equals(String.valueOf(devicetype))) {
				schoolDiscounts.setDiscountpackage(discountpackage);
			}
			schoolDiscounts.setDiscountprice(price);
			schoolDiscounts.setSchoolid(schoolid);
			schoolDiscountsMapper.updateByExampleSelective(schoolDiscounts, schoolDiscountsExample);
		} else {
			SchoolDiscounts schoolDiscounts = new SchoolDiscounts();
			schoolDiscounts.setCreatetime(new Date());
			schoolDiscounts.setUpdatetime(new Date());
			schoolDiscounts.setDevicetype(devicetype);
			if ("2".equals(String.valueOf(devicetype))) {
				schoolDiscounts.setDiscountpackage(discountpackage);
			}
			schoolDiscounts.setDiscountprice(price);
			schoolDiscounts.setSchoolid(schoolid);
			schoolDiscountsMapper.insertSelective(schoolDiscounts);
		}
	}

	private void updateSchoolDiscount(BigDecimal price, Integer devicetype, Integer schoolid,
			BigDecimal discountpackage) {
		SchoolDiscounts schoolDiscounts = new SchoolDiscounts();
		schoolDiscounts.setCreatetime(new Date());
		schoolDiscounts.setUpdatetime(new Date());
		schoolDiscounts.setDevicetype(devicetype);
		if ("2".equals(String.valueOf(devicetype))) {
			schoolDiscounts.setDiscountpackage(discountpackage);
		}
		schoolDiscounts.setDiscountprice(price);
		schoolDiscounts.setSchoolid(schoolid);
		SchoolDiscountsExample schoolDiscountsExample = new SchoolDiscountsExample();
		schoolDiscountsExample.createCriteria().andSchoolidEqualTo(schoolid).andDevicetypeEqualTo(devicetype);
		schoolDiscountsMapper.updateByExampleSelective(schoolDiscounts, schoolDiscountsExample);
	}

	/**
	 * <p>
	 * Title: updateSchoolPrices
	 * </p>
	 * <p>
	 * 功能描述: 修改学校价格,和新增学校价格一样
	 * </p>
	 * 
	 * @param schoolDiscountVo
	 * @return
	 */
	@Override
	@Transactional
	public String updateSchoolPrices(SchoolDiscountVO schoolDiscountVo) {
		if (schoolDiscountVo == null || StringUtils.isBlank(schoolDiscountVo.getSchoolName())) {
			return "学校名称为空";
		}
		Integer schoolid = getSchoolidBySchoolName(schoolDiscountVo.getSchoolName());
		if (schoolid == null) {
			return "该学校不存在";
		}
		if (schoolDiscountVo.getSmartCardPrice() != null) {
			updateSchoolDiscount(schoolDiscountVo.getSmartCardPrice(), 2, schoolid, schoolDiscountVo.getPackagePrice());
			synPackageToDevices(schoolDiscountVo.getSchoolName(), schoolDiscountVo.getPackagePrice());
		} else if (schoolDiscountVo.getPackagePrice() != null) {
			updateSchoolDiscount(null, 2, schoolid, schoolDiscountVo.getPackagePrice());
			synPackageToDevices(schoolDiscountVo.getSchoolName(), schoolDiscountVo.getPackagePrice());
		}
		// 新增13.56考勤卡价格
		if (schoolDiscountVo.getWhiteCard13_56Price() != null) {
			updateSchoolDiscount(schoolDiscountVo.getWhiteCard13_56Price(), 8, schoolid, null);
		}
		// 新增2.4考勤卡价格
		if (schoolDiscountVo.getWhiteCard2_4Price() != null) {
			updateSchoolDiscount(schoolDiscountVo.getWhiteCard2_4Price(), 9, schoolid, null);
		}
		// 清除缓存
		cacheService.flushDevicePrices(schoolDiscountVo.getSchoolName());
		return null;
	}

	private Integer getSchoolidBySchoolName(String schoolName) {
		SchoolinfoExample schoolinfoExample = new SchoolinfoExample();
		schoolinfoExample.createCriteria().andSchoolnameEqualTo(schoolName);
		List<Schoolinfo> schoolList = schoolinfoMapper.selectByExample(schoolinfoExample);
		if (CollectionUtils.isEmpty(schoolList)) {
			return null;
		} else {
			return schoolList.get(0).getId();
		}
	}

	/**
	 * <p>
	 * Title: deleteSchoolPrices
	 * </p>
	 * <p>
	 * 功能描述: 删除学校价格，物理删除 1.如果只传schoolName，不传devicetype,则删除该学校下的所有促销价格和套餐
	 * 2.传schoolName和devicetype，则删除对应学校下对应设备类型的促销价格和套餐
	 * 这里会同步该学校下的所有设备的价格，如果设备的价格与当前学校设置的价格相同，则将价格置为0，若不相同，证明是单独设置的价格，则不管
	 * </p>
	 * 
	 * @param schoolDiscountVo
	 * @return
	 */
	@Override
	@Transactional
	public String deleteSchoolPrices(SchoolDiscountVO schoolDiscountVo) {
		if (schoolDiscountVo == null || StringUtils.isBlank(schoolDiscountVo.getSchoolName())) {
			return "学校名称为空";
		}
		Integer schoolid = getSchoolidBySchoolName(schoolDiscountVo.getSchoolName());
		if (schoolid == null) {
			return "该学校不存在";
		}
		SchoolDiscountsExample schoolDiscountsExample = new SchoolDiscountsExample();
		if (schoolDiscountVo.getDevicetype() != null) {
			List<SchoolDiscountDO> schoolDiscountList = devicePriceServiceDAO
					.getSchoolPriceByName(schoolDiscountVo.getSchoolName());
			BigDecimal packagePrice = null;
			if (!CollectionUtils.isEmpty(schoolDiscountList)) {
				for (SchoolDiscountDO schoolDiscountDo : schoolDiscountList) {
					if (schoolDiscountVo.getDevicetype().toString()
							.equals(String.valueOf(schoolDiscountDo.getDevicetype()))) {
						packagePrice = schoolDiscountDo.getDiscountpackage();
					}
				}
			}
			// 清除devicelists表
			deleteDevicelistPackage(schoolDiscountVo.getSchoolName(), packagePrice);
			// 清除std_deviceinfo表
			deleteStdDeviceinfoPackage(schoolDiscountVo.getSchoolName(), packagePrice);
			// 清除std_iot_device表
			if ("2".equals(String.valueOf(schoolDiscountVo.getDevicetype()))) {
				deleteStdIotDevicePackage(schoolDiscountVo.getSchoolName(), packagePrice);
			}

			schoolDiscountsExample.createCriteria().andSchoolidEqualTo(schoolid)
					.andDevicetypeEqualTo(schoolDiscountVo.getDevicetype());
			schoolDiscountsMapper.deleteByExample(schoolDiscountsExample);
		} else {
			List<SchoolDiscountDO> schoolDiscountList = devicePriceServiceDAO
					.getSchoolPriceByName(schoolDiscountVo.getSchoolName());
			if (!CollectionUtils.isEmpty(schoolDiscountList)) {
				for (SchoolDiscountDO schoolDiscountDo : schoolDiscountList) {
					// 清除devicelists表
					deleteDevicelistPackage(schoolDiscountVo.getSchoolName(), schoolDiscountDo.getDiscountpackage());
					// 清除std_deviceinfo表
					deleteStdDeviceinfoPackage(schoolDiscountVo.getSchoolName(), schoolDiscountDo.getDiscountpackage());
					// 清除std_iot_device表
					if ("2".equals(String.valueOf(schoolDiscountDo.getDevicetype()))) {
						deleteStdIotDevicePackage(schoolDiscountVo.getSchoolName(),
								schoolDiscountDo.getDiscountpackage());
					}
				}
			}

			schoolDiscountsExample.createCriteria().andSchoolidEqualTo(schoolid);
			schoolDiscountsMapper.deleteByExample(schoolDiscountsExample);
		}
		// 清除缓存
		cacheService.flushDevicePrices(schoolDiscountVo.getSchoolName());
		return null;
	}

	private void deleteStdIotDevicePackage(String schoolName, BigDecimal packagePrice) {
		Map<String, Object> map = new HashMap<>();
		map.put("schoolName", schoolName);
		map.put("packagePrice", packagePrice);
		map.put("defaultPackagePrice", getDefaultPackagePrice());
		devicePriceServiceDAO.deleteStdIotDevicePackageBySchoolName(map);
	}

	private void deleteStdDeviceinfoPackage(String schoolName, BigDecimal packagePrice) {
		Map<String, Object> map = new HashMap<>();
		map.put("schoolName", schoolName);
		map.put("deviceType", 2);
		List<Devicelist> devicelistList = devicePriceServiceDAO.getDevicelistsByParam(map);
		if (!CollectionUtils.isEmpty(devicelistList)) {
			map = new HashMap<>();
			map.put("devicelistList", devicelistList);
			map.put("packagePrice", packagePrice);
			map.put("defaultPackagePrice", getDefaultPackagePrice());
			devicePriceServiceDAO.deleteStdDeviceinfoPackageBySchoolName(map);
		}
	}

	private void deleteDevicelistPackage(String schoolName, BigDecimal packagePrice) {
		Map<String, Object> map = null;
		if (packagePrice.compareTo(new BigDecimal("0")) == 1) {
			map = new HashMap<>();
			map.put("schoolName", schoolName);
			map.put("packagePrice", packagePrice);
			map.put("defaultPackagePrice", getDefaultPackagePrice());
			devicePriceServiceDAO.deleteDeviceListPackageBySchoolName(map);
		}
	}
	

	/**
	 * <p>
	 * Title: getDevicePrice
	 * </p>
	 * <p>
	 * 功能描述: 获取单个设备的价格，包括硬件价格，套餐价格
	 * </p>
	 * 
	 * @param mdtid
	 * @return
	 */
	@Override
	public Object getDevicePrice(String mdtid) {
		if (StringUtils.isBlank(mdtid)) {
			return "设备号为空";
		}
		if (mdtid.length() != 14) {
			return "设备号长度不是14位";
		}
		// 先判断设备是否存在
		if (!checkDeviceExist(mdtid)) {
			return "该设备不存在";
		}
		
		// 获取设备套餐
		StdIotDeviceExample stdIotDeviceExample = new StdIotDeviceExample();
		stdIotDeviceExample.createCriteria().andImeiLike(mdtid + "%");
		List<StdIotDevice> stdIotDeviceList = stdIotDeviceMapper.selectByExample(stdIotDeviceExample);
		BigDecimal packagePrice = null, devicePrice = null;
		if (!CollectionUtils.isEmpty(stdIotDeviceList)) {
			packagePrice = stdIotDeviceList.get(0).getFee2();
			devicePrice = stdIotDeviceList.get(0).getFee();
		}else {
			DevicelistExample devicelistExample = new DevicelistExample();
			devicelistExample.createCriteria().andMdtidEqualTo(mdtid);
			List<Devicelist> devicelistList = devicelistMapper.selectByExample(devicelistExample);
			if(!CollectionUtils.isEmpty(devicelistList)) {
				packagePrice = devicelistList.get(0).getFee2();
				devicePrice = devicelistList.get(0).getFee();
			}
		}
		DevicePriceVO devicePriceVo = new DevicePriceVO();
		devicePriceVo.setMdtid(mdtid);
		devicePriceVo.setPackagePrice(packagePrice);
		devicePriceVo.setDevicePrice(devicePrice);
		return devicePriceVo;
		
		
		/*
		// 判断该设备是否存在
		String schoolName = getSchoolNameByMdtid(mdtid);
		DevicePriceVO devicePriceVo = new DevicePriceVO();
		if (schoolName == null) {
			return getDefaultPrice(mdtid);
		} else {
			List<SchoolDiscountVO> schoolDiscountVoList = getSchoolPrices(schoolName);
			if (CollectionUtils.isEmpty(schoolDiscountVoList)) {
				return getDefaultPrice(mdtid);
			} else {
				devicePriceVo.setMdtid(mdtid);
				Integer deviceType = getDeviceTypeByMdtid(mdtid);
				boolean isTrue = false;
				for (SchoolDiscountVO schoolDiscountVo : schoolDiscountVoList) {
					if (deviceType.toString().equals(String.valueOf(schoolDiscountVo.getDevicetype()))) {
						isTrue = true;
						devicePriceVo.setDevicePrice(schoolDiscountVo.getPrice());
						if (schoolDiscountVo.getPrice() != null
								&& schoolDiscountVo.getPrice().compareTo(new BigDecimal("0")) == 1) {
							devicePriceVo.setDevicePrice(schoolDiscountVo.getPrice());
						} else {
							devicePriceVo.setDevicePrice(getDefaultDevicePrice(deviceType));
						}
						
						 * if ("2".equals(deviceType.toString())) { if
						 * (schoolDiscountVo.getPackagePrice().compareTo(new
						 * BigDecimal("0")) == 1) {
						 * devicePriceVo.setPackagePrice(schoolDiscountVo.
						 * getPackagePrice()); } else {
						 * devicePriceVo.setPackagePrice(getDefaultPackagePrice(
						 * )); } } else { devicePriceVo.setPackagePrice(new
						 * BigDecimal("0")); }
						 
						break;
					}
				}
				if (!isTrue) {
					return getDefaultPrice(mdtid);
				} else {
					// 获取设备套餐
					StdIotDeviceExample stdIotDeviceExample = new StdIotDeviceExample();
					stdIotDeviceExample.createCriteria().andImeiLike(mdtid + "%");
					List<StdIotDevice> stdIotDeviceList = stdIotDeviceMapper.selectByExample(stdIotDeviceExample);
					BigDecimal packagePrice = null;
					if (!CollectionUtils.isEmpty(stdIotDeviceList)) {
						packagePrice = stdIotDeviceList.get(0).getFee2();
					}
					devicePriceVo.setPackagePrice(packagePrice);
					if (packagePrice == null || packagePrice.compareTo(new BigDecimal("0")) != 1) {
						devicePriceVo.setPackagePrice(getDefaultPackagePrice());
					} else {
						devicePriceVo.setPackagePrice(packagePrice);
					}
				}
			}
		}*/
	}

	private Integer getDeviceTypeByMdtid(String mdtid) {
		Integer deviceType = null;
		if (mdtid.startsWith("8888")) {
			deviceType = 8;
		} else if (mdtid.startsWith("9999")) {
			deviceType = 9;
		} else {
			deviceType = 2;
		}
		return deviceType;
	}

	private BigDecimal getDefaultDevicePrice(Integer deviceType) {
		SystemPriceVO systemPrice = getSystemPrice();
		if (systemPrice == null) {
			return null;
		}
		switch (deviceType) {
		case 2:
			return systemPrice.getSmartCardPrice();
		case 8:
			return systemPrice.getWhiteCard13_56Price();
		case 9:
			return systemPrice.getWhiteCard2_4Price();
		}
		return null;
	}
	
	@Override
	public BigDecimal getDefaultPackagePrice() {
		SystemPriceVO systemPrice = getSystemPrice();
		if (systemPrice == null) {
			return null;
		}
		return systemPrice.getPackagePrice();
	}

	private DevicePriceVO getDefaultPrice(String mdtid) {
		DevicePriceVO devicePriceVo = new DevicePriceVO();
		devicePriceVo.setMdtid(mdtid);
		// 获取设备的默认价格
		SystemPriceVO systemPrice = getSystemPrice();
		if (systemPrice == null) {
			return devicePriceVo;
		}
		if (mdtid.startsWith("8888")) {
			devicePriceVo.setDevicePrice(systemPrice.getWhiteCard13_56Price());
			devicePriceVo.setPackagePrice(new BigDecimal("0"));
		} else if (mdtid.startsWith("9999")) {
			devicePriceVo.setDevicePrice(systemPrice.getWhiteCard2_4Price());
			devicePriceVo.setPackagePrice(new BigDecimal("0"));
		} else {
			devicePriceVo.setDevicePrice(systemPrice.getSmartCardPrice());
			devicePriceVo.setPackagePrice(getSmartCardPackagePrice(mdtid));
		}
		return devicePriceVo;
	}

	private BigDecimal getSmartCardPackagePrice(String mdtid) {
		StdIotDeviceExample stdIotDeviceExample = new StdIotDeviceExample();
		stdIotDeviceExample.createCriteria().andImeiLike(mdtid + "%");
		List<StdIotDevice> stdIotDeviceList = stdIotDeviceMapper.selectByExample(stdIotDeviceExample);
		if (CollectionUtils.isEmpty(stdIotDeviceList)) {
			DevicelistExample devicelistExample = new DevicelistExample();
			devicelistExample.createCriteria().andMdtidEqualTo(mdtid);
			List<Devicelist> devicelistList = devicelistMapper.selectByExample(devicelistExample);
			if (CollectionUtils.isEmpty(devicelistList)) {
				return null;
			} else {
				BigDecimal packagePrice = devicelistList.get(0).getFee2();
				return packagePrice;
				/*if (packagePrice == null || packagePrice.compareTo(new BigDecimal("0")) == 0) {
					SystemPriceVO systemPrice = getSystemPrice();
					return systemPrice.getPackagePrice();
				} else {
					return packagePrice;
				}*/
			}
		} else {
			BigDecimal packagePrice = stdIotDeviceList.get(0).getFee2();
			return packagePrice;
			/*if (packagePrice == null || packagePrice.compareTo(new BigDecimal("0")) == 0) {
				SystemPriceVO systemPrice = getSystemPrice();
				return systemPrice.getPackagePrice();
			} else {
				return packagePrice;
			}*/
		}
	}

	private boolean checkDeviceExist(String mdtid) {
		if (mdtid.startsWith("8888") || mdtid.startsWith("9999")) {
			DevicelistExample devicelistExample = new DevicelistExample();
			devicelistExample.createCriteria().andMdtidEqualTo(mdtid);
			if (CollectionUtils.isEmpty(devicelistMapper.selectByExample(devicelistExample))) {
				return false;
			} else {
				return true;
			}
		} else {
			StdIotDeviceExample stdIotDeviceExample = new StdIotDeviceExample();
			stdIotDeviceExample.createCriteria().andImeiLike(mdtid + "%");
			if (CollectionUtils.isEmpty(stdIotDeviceMapper.selectByExample(stdIotDeviceExample))) {
				DevicelistExample devicelistExample = new DevicelistExample();
				devicelistExample.createCriteria().andMdtidEqualTo(mdtid);
				if (CollectionUtils.isEmpty(devicelistMapper.selectByExample(devicelistExample))) {
					return false;
				} else {
					return true;
				}
			} else {
				return true;
			}
		}
	}

	private String getSchoolNameByMdtid(String mdtid) {
		if (mdtid.startsWith("8888") || mdtid.startsWith("9999")) {
			DevicelistExample devicelistExample = new DevicelistExample();
			devicelistExample.createCriteria().andMdtidEqualTo(mdtid);
			List<Devicelist> devicelistList = devicelistMapper.selectByExample(devicelistExample);
			if (!CollectionUtils.isEmpty(devicelistList)) {
				return devicelistList.get(0).getSchoolname();
			}
		} else {
			StdIotDeviceExample stdIotDeviceExample = new StdIotDeviceExample();
			stdIotDeviceExample.createCriteria().andImeiLike(mdtid + "%");
			List<StdIotDevice> stdIotDeviceList = stdIotDeviceMapper.selectByExample(stdIotDeviceExample);
			if (!CollectionUtils.isEmpty(stdIotDeviceList)) {
				return stdIotDeviceList.get(0).getSchoolname();
			}
		}
		return null;
	}

	/**
	 * <p>
	 * Title: saveDevicePackagePrice
	 * </p>
	 * <p>
	 * 功能描述: 修改单个设备的设备套餐
	 * </p>
	 * 
	 * @param mdtid
	 * @param packagePrice
	 * @return
	 */
	@Override
	public String saveDevicePackagePrice(String mdtid, BigDecimal packagePrice) {
		if (StringUtils.isBlank(mdtid)) {
			return "设备号为空";
		}
		if (packagePrice == null) {
			return "套餐价格为空";
		}
		if (mdtid.length() != 14) {
			return "设备号不是14位";
		}
		Integer deviceType = getDeviceTypeByMdtid(mdtid);
		if (!"2".equals(String.valueOf(deviceType))) {
			return "只能设置智能学生证的套餐价格";
		}
		if (!checkDeviceExist(mdtid)) {
			return "设备号不存在";
		}
		updateDevicePackagePrice(mdtid, packagePrice);
		// 清除缓存
		cacheService.flushPackagePrice(mdtid);
		flushBabysByMdtid(mdtid);
		return null;
	}

	@Transactional
	private void updateDevicePackagePrice(String mdtid, BigDecimal packagePrice) {
		// 修改std_deviceinfo表
		StdDeviceinfo stdDeviceinfo = new StdDeviceinfo();
		stdDeviceinfo.setFee2(packagePrice);
		stdDeviceinfo.setUpdatetime(new Date());
		StdDeviceinfoExample stdDeviceinfoExample = new StdDeviceinfoExample();
		stdDeviceinfoExample.createCriteria().andMdtidEqualTo(mdtid);
		stdDeviceinfoMapper.updateByExampleSelective(stdDeviceinfo, stdDeviceinfoExample);

		// 修改devicelists表
		DevicelistExample devicelistExample = new DevicelistExample();
		devicelistExample.createCriteria().andMdtidEqualTo(mdtid);
		Devicelist devicelist = new Devicelist();
		devicelist.setUpdatetime(new Date());
		devicelist.setFee2(packagePrice);
		devicelistMapper.updateByExampleSelective(devicelist, devicelistExample);

		// 修改std_iot_device表
		Map<String, Object> map = new HashMap<>();
		map.put("packagePrice", packagePrice);
		map.put("mdtid", mdtid);
		devicePriceServiceDAO.updateStdIotDevicePrices(map);
	}

	/**
	 * <p>
	 * Title: deleteDevicePackagePrice
	 * </p>
	 * <p>
	 * 功能描述: 删除单个设备的套餐价格,删除之后，该设备的套餐价格就是系统的默认价格
	 * </p>
	 * 
	 * @param mdtid
	 * @return
	 */
	@Override
	public String deleteDevicePackagePrice(String mdtid) {
		if (StringUtils.isBlank(mdtid)) {
			return "设备号为空";
		}
		if (mdtid.length() != 14) {
			return "设备号不是14位";
		}
		Integer deviceType = getDeviceTypeByMdtid(mdtid);
		if (!"2".equals(String.valueOf(deviceType))) {
			return "只能删除智能学生证的套餐价格";
		}
		if (!checkDeviceExist(mdtid)) {
			return "设备号不存在";
		}
		updateDevicePackagePrice(mdtid, getDefaultPackagePrice());
		// 清除缓存
		cacheService.flushPackagePrice(mdtid);
		flushBabysByMdtid(mdtid);
		return null;
	}

	private void flushBabysByMdtid(String mdtid) {
		ParentInfo parent = new ParentInfo();
		parent.setMdtid(mdtid);
		List<ParentInfo> parentList = parentManageDAO.queryByIdOrTelOrImei(parent);
		if (!CollectionUtils.isEmpty(parentList)) {
			List<Integer> peopleIdList = new ArrayList<>();
			for (ParentInfo parentInfo : parentList) {
				peopleIdList.add(parentInfo.getPeopleid());
			}
			if (!CollectionUtils.isEmpty(peopleIdList)) {
				cacheService.flushBabys(StringUtils.join(peopleIdList, ","));
			}
		}
	}

	@Override
	public DevicePriceVO getDevicePriceVo(String mdtid) {
		DevicePriceVO devicePriceVo = null;
		Object object = getDevicePrice(mdtid);
		if (object instanceof DevicePriceVO) {
			devicePriceVo = (DevicePriceVO) object;
		}
		if (devicePriceVo != null) {
			return devicePriceVo;
		} else {
			return new DevicePriceVO();
		}
	}

	@Override
	public BigDecimal getSchoolPackagePrice(String schoolName) {
		List<SchoolDiscountVO> schoolPriceList = getSchoolPrices(schoolName);
		if (!CollectionUtils.isEmpty(schoolPriceList)) {
			for (SchoolDiscountVO schoolDiscountVo : schoolPriceList) {
				if ("2".equals(String.valueOf(schoolDiscountVo.getDevicetype()))
						&& schoolDiscountVo.getPackagePrice() != null
						&& schoolDiscountVo.getPackagePrice().compareTo(new BigDecimal("0")) == 1) {
					return schoolDiscountVo.getPackagePrice();
				}
			}
		}
		return null;
	}
}
